<template>
  <el-dialog :title="!dataForm.id ? $t('Common.Add') : $t('Common.Edit')" :close-on-click-modal="false" :visible.sync="visible" v-if="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
      <el-form-item label="分类" prop="parentId">
        <el-cascader :options="floorDataList" v-model="selectParentId" @change="floorDataListChange">
          <template slot-scope="{ node, data }">
            <span>{{ data.label }}</span>
            <span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
          </template>
        </el-cascader>


        <!--     <el-select v-model="dataForm.parentId" filterable placeholder="请选择分类">
       <el-option
         v-for="item in floorDataList"
         :key="item.id"
         :label="item.parentId == 0 ? `${item.name}` : `══ ${item.name}`"
         :value="item.id">
       </el-option>
      </el-select> -->
      </el-form-item>
      <el-form-item label="单位" prop="companyId">
        <el-select v-model="dataForm.companyId" placeholder="请选择商品单位">
          <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-row type="flex" class="row-bg">
        <el-col :span="12">
          <el-form-item label="编号" prop="shopNumber">
            <el-input v-model="dataForm.shopNumber" placeholder="编号"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="名称" prop="shopName">
            <el-input v-model="dataForm.shopName" placeholder="名称"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex" class="row-bg">
        <el-col :span="12">
          <el-form-item label="进价" prop="purchasePrice">
            <el-input-number v-model="dataForm.purchasePrice" :precision="2" :step="1" :min="0"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="售价" prop="salePrice">
            <el-input-number v-model="dataForm.salePrice" :precision="2" :step="1" :min="0"></el-input-number>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex" class="row-bg">
        <el-col :span="12">
          <el-form-item label="进货量" prop="purchaseNum">
            <el-input-number v-model="dataForm.purchaseNum" :step="1" step-strictly :min="0"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="销售量" prop="saleNum">
            <el-input-number v-model="dataForm.saleNum" :step="1" step-strictly :min="0"></el-input-number>
          </el-form-item>
        </el-col>
      </el-row>


      <el-form-item label="库存量" prop="stockNum">
        <el-input-number v-model="dataForm.stockNum" :step="1" step-strictly :min="0"></el-input-number>
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-switch v-model="dataForm.state" :active-value="1" :inactive-value="0" active-text="启用" class="switch"
          inactive-text="禁用"></el-switch>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{ $t('Common.Cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmit()">{{ $t('Common.Confirm') }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data() {
      return {
        selectParentId: [],
        options: [],
        floorDataList: [],
        visible: false,
        dataForm: {
          id: 0,
          consumelist: '-1',
          sellerId: '',
          parentId: '',
          companyId: '',
          shopNumber: '',
          shopName: '',
          purchasePrice: 0,
          salePrice: 0,
          purchaseNum: 0,
          saleNum: 0,
          stockNum: 0,
          creatTime: '',
          updateTime: '',
          state: 1
        },
        dataRule: {
          sellerId: [{
            required: true,
            message: '商家不能为空',
            trigger: 'blur'
          }],
          parentId: [{
            required: true,
            message: '上级不能为空',
            trigger: 'blur'
          }],
          companyId: [{
            required: true,
            message: '单位id不能为空',
            trigger: 'blur'
          }],
          shopNumber: [{
            required: true,
            message: '编号不能为空',
            trigger: 'blur'
          }],
          shopName: [{
            required: true,
            message: '名称不能为空',
            trigger: 'blur'
          }],
          purchasePrice: [{
            required: true,
            message: '进价不能为空',
            trigger: 'blur'
          }],
          salePrice: [{
            required: true,
            message: '售价不能为空',
            trigger: 'blur'
          }],
          purchaseNum: [{
            required: true,
            message: '进货量不能为空',
            trigger: 'blur'
          }],
          saleNum: [{
            required: true,
            message: '销售量不能为空',
            trigger: 'blur'
          }],
          stockNum: [{
            required: true,
            message: '库存量不能为空',
            trigger: 'blur'
          }],
          state: [{
            required: true,
            message: '状态(0禁用1启用)不能为空',
            trigger: 'blur'
          }]
        }
      }
    },
    methods: {
      floorDataListChange(val) {
        console.log(val)
        this.dataForm.parentId = val[val.length - 1]
      },
      consumeChange() {
        if (this.dataForm.consumelist != '-1') {
          this.dataForm.parentId = this.dataForm.consumelist[this.dataForm.consumelist.length - 1];
        } else {
          this.dataForm.parentId = '-1';
        }
      },
      //获取所有分类
      getFloorLists() {
        this.$http({
          url: this.$http.adornUrl('/hotel/thotelfloorroomconsume/data2'),
          method: 'get',
          params: this.$http.adornParams({})
        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            // this.floorDataList = data.data;
            this.floorDataList = data.data
            // this.floorDataList = data.data;
          } else {
            this.floorDataList = [];
          }
        });
      },
      //获取所有商品单位
      getDanWeiLists() {
        this.$http({
          url: this.$http.adornUrl('/hotel/thotelfloorroomconsumecompany/list'),
          method: 'get',
          params: this.$http.adornParams({
            page: 1,
            limit: 999,
            state: 1
          })
        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            this.options = data.page.list;
          } else {
            this.options = [];
          }
        });
      },
      init(id) {

        this.dataForm.id = id || 0
        this.visible = true

        //this.$refs['dataForm'].resetFields();
        // this.dataForm={};
        // console.log(this.dataForm);

        this.$nextTick(() => {
          if (id) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/thotelfloorroomconsumeshop/info/` + id),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({
              data
            }) => {
              if (data && data.code === 0) {
                this.getDanWeiLists();
                this.getFloorLists();
                this.dataForm.sellerId = data.tHotelFloorRoomConsumeShop.sellerId
                this.dataForm.id = id || 0
                this.dataForm.parentId = data.tHotelFloorRoomConsumeShop.parentId
                this.dataForm.companyId = data.tHotelFloorRoomConsumeShop.companyId
                this.dataForm.shopNumber = data.tHotelFloorRoomConsumeShop.shopNumber
                this.dataForm.shopName = data.tHotelFloorRoomConsumeShop.shopName
                this.dataForm.purchasePrice = data.tHotelFloorRoomConsumeShop.purchasePrice
                this.dataForm.salePrice = data.tHotelFloorRoomConsumeShop.salePrice
                this.dataForm.purchaseNum = data.tHotelFloorRoomConsumeShop.purchaseNum
                this.dataForm.saleNum = data.tHotelFloorRoomConsumeShop.saleNum
                this.dataForm.stockNum = data.tHotelFloorRoomConsumeShop.stockNum
                this.dataForm.creatTime = data.tHotelFloorRoomConsumeShop.creatTime
                this.dataForm.updateTime = data.tHotelFloorRoomConsumeShop.updateTime
                this.dataForm.state = data.tHotelFloorRoomConsumeShop.state

              }
            })
          } else {
            this.getDanWeiLists();
            this.getFloorLists();
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
       var lists=this.options.filter(item=>item.id==this.dataForm.companyId)
        console.log("测试表单" + this.dataForm);
        this.$refs['dataForm'].validate((valid) => {
          console.log("测试表单" + valid);
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(
                `/hotel/thotelfloorroomconsumeshop/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'sellerId': this.dataForm.sellerId,
                'parentId': this.dataForm.parentId,
                'companyId': this.dataForm.companyId,
                'shopNumber': this.dataForm.shopNumber,
                'shopName': this.dataForm.shopName,
                'purchasePrice': this.dataForm.purchasePrice,
                'salePrice': this.dataForm.salePrice,
                'purchaseNum': this.dataForm.purchaseNum,
                'saleNum': this.dataForm.saleNum,
                'stockNum': this.dataForm.stockNum,
                'creatTime': this.dataForm.creatTime,
                'updateTime': this.dataForm.updateTime,
                'state': this.dataForm.state,
                'companyname':lists[0].name
              })
            }).then(({
              data
            }) => {
              if (data && data.code === 0) {
                this.$message({
                  message: this.$t('Common.OperationSuccessful'),
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
